#!/bin/bash

verbose=0

usage()
{
        echo "Usage: $0 [-v] [-h] -i infile"
}

dispatch()
{
        dir=$(mktemp -d)
        if [ $? -ne 0 ]; then
                echo "mktemp error"
                return $?
        fi

        tar xf $1 -C $dir > /dev/null 2>&1
        if [ $? -ne 0 ]; then
                echo "tar error"
                return $?
        fi

        for license in $(find $dir -name "*.license"); do
                echo "dispatch $(basename $license)"

                ip=$(basename $license)
                ip=${ip%.*}

                scp $license root@$ip:/opt/fusionstack/license
                if [ $? -ne 0 ]; then
                        echo "error"
                        return $?
                fi

                ssh root@$ip '/opt/fusionstack/lich/libexec/lich.license -m list'
        done

        rm -rf $dir
}

while getopts vhi: option; do
        case $option in
                v)
                        verbose=1
                        break
                        ;;
                h)
                        usage
                        exit 0
                        ;;
                i)
                        infile="$OPTARG"
                        ;;
                ?)
                        usage
                        exit 1
                        ;;
        esac
done

if [ -z "$infile" ]; then
        usage
        exit 2
fi

dispatch $infile
exit $?
